﻿using CoreShop.Library;
using CoreShop.Model;
using CoreShop.Services;
using SqlSugar;
using SqlSugar.Extensions;
using Sunny.UI;
using System.Collections.Generic;
using System.Security.Cryptography;
using System.Windows.Forms;

namespace CoreShop.PrintService
{
    public partial class frmPrinterState : UIForm
    {
        public List<corecmsprintscheme> lstSource = null;

        public frmPrinterState()
        {
            InitializeComponent();
        }

        private void FPrinterManage_Shown(object sender, System.EventArgs e)
        {
            lstSource = new corecmsprintschemeServices().QueryAsync(0);
            foreach (var cheme in lstSource)
            {
                UIPrinterItem item = new UIPrinterItem();
                item.Name = GetPrinterItemName(cheme.id);
                item.PrinterName = cheme.printerName;
                item.Desc1 = EnumHelper.GetEnumByValue<GlobalEnumVars.PrinterBrand>(cheme.brandId.ObjToInt(0)) +
                    "/" + EnumHelper.GetEnumByValue<GlobalEnumVars.paperType>(cheme.paperType.ObjToInt(0));
                item.Desc2 = cheme.region;
                //item.ShowLogo = true;
                item.IP = "IP: " + cheme.printerIP;
                item.Linked = cheme.Linked;
                item.Image = Properties.Resources.p2;
                item.ShowEditNetwork = false;
                item.EditNetwork += Item_EditNetwork;
                item.ClickOperation += Item_ClickOperation;
                item.PrintTestPage += Item_PrintTestPage;
                layPrinterList.Add(item);
            }
        }

        private string GetPrinterItemName(long printerId)
        {
            return "uiPrinterItem_" + printerId.ToString();
        }

        private bool GetPrinterState(string printerIp)
        {
            var linkEd = false;
            JsPrinter m_printer = new JsPrinter();
            m_printer.OpenNetSev();
            m_printer.m_Inited = m_printer.ConnectNetPort(printerIp);
            if (m_printer.m_Inited > 0) linkEd = true;
            m_printer.CloseNetPor();
            return linkEd;
        }

        private void GetPrinterState()
        {
            foreach (var cheme in lstSource)
            {                
                cheme.Linked = GetPrinterState(cheme.printerIP);
                var mControl = layPrinterList.Get(GetPrinterItemName(cheme.id));
                var item = (UIPrinterItem)mControl;
                item.Linked = cheme.Linked;                
                if (item.Linked)
                    item.Image = Properties.Resources.p2;
                else
                    item.Image = Properties.Resources.p1;
                item.Refresh();
            }       
        }

        private void Add_AddPrinter(object sender, System.EventArgs e)
        {
            ShowInfoTip("添加打印机");
        }

        private void Item_PrintTestPage(object sender, System.EventArgs e)
        {
            var item = (UIPrinterItem)sender;
            ShowInfoTip(item.PrinterName + ", 打印测试单");
        }

        private void Item_ClickOperation(object sender, System.EventArgs e)
        {
            var item = (UIPrinterItem)sender;
            ShowInfoTip(item.PrinterName + ", 点击操作");
        }

        private void Item_EditNetwork(object sender, System.EventArgs e)
        {
            var item = (UIPrinterItem)sender;
            ShowInfoTip(item.PrinterName + ", 修改网段");
        }

        private void btnClose_Click(object sender, System.EventArgs e)
        {
            this.Close();
        }

        private void btnRefreshPrint_Click(object sender, System.EventArgs e)
        {
            GetPrinterState();
        }
    }
}
